package cn.maihe.elg.operation.web.seller;

import cn.maihe.elg.operation.exception.ErrorCode;
import cn.maihe.elg.operation.exception.SystemRuntimeException;
import cn.maihe.elg.operation.model.enums.UserType;
import cn.maihe.elg.operation.repository.entity.OrganizationDO;
import cn.maihe.elg.operation.repository.entity.SellerInfoDO;
import cn.maihe.elg.operation.repository.entity.SellerInfoDOCondition;
import cn.maihe.elg.operation.repository.entity.SellerInfoVo;
import cn.maihe.elg.operation.security.model.UserContext;
import cn.maihe.elg.operation.service.invoice.SellerInfoService;
import cn.maihe.elg.operation.service.system.OrganizationService;
import cn.maihe.elg.operation.supports.invoice.nuonuo.InvoiceHelper;
import cn.maihe.elg.operation.utils.UserContextUtil;
import cn.maihe.framework.starter.base.sql.PageBo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description 销方信息controller
 * @Author caixh
 * @Date 2021/4/14.
 */
@Api(value = "销方信息管理", tags = "销方信息管理")
@RestController
@RequestMapping("/api")
@Slf4j
public class SellerInfoController {
    @Resource
    private SellerInfoService sellerInfoService;
    @Resource
    private OrganizationService organizationService;
    @Resource
    private InvoiceHelper invoiceHelper;

    @ApiOperation(value = "分页查询销方信息", notes = "根据开票信息类型、机构编码、机构名称 分页查询")
    @RequestMapping(value = "/sellerInfos", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query", required = true),
//            @ApiImplicitParam(name = "Name", value = "开票信息名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "gmtCreateRange", value = "创建时间（startDate,endDate）", dataType = "String", paramType = "query")
    })
    public PageBo<SellerInfoDO> pageList(int pageNo, int pageSize, String gmtCreateRange, SellerInfoVo vo) {
        //登录机构过滤
        UserContext uc= UserContextUtil.getUserContext();
        if(UserType.GENERAL.getType() != uc.getUserType()){
            vo.setOrgId(uc.getOrgId());
        }
        return sellerInfoService.selectPageBySql(pageNo, pageSize, gmtCreateRange, vo);
    }
    @ApiOperation(value = "获取销方信息", notes = "根据销方信息id获取销方信息")
    @ApiImplicitParam(name = "id", value = "开票信息id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/sellerInfo", method = RequestMethod.GET)
    public SellerInfoDO getSeller() {
        String id = "";
        UserContext uc= UserContextUtil.getUserContext();
        if(UserType.GENERAL.getType() != uc.getUserType()){
            id = uc.getOrgId().toString();
        }else{
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "销方信息");
        }

        SellerInfoDO curDO = sellerInfoService.selectOne(SellerInfoDOCondition.builder().andOrgIdEq(uc.getOrgId()).build());
        if (curDO == null) {
           curDO = new SellerInfoDO();
            OrganizationDO organizationDO = organizationService.getOrganization(uc.getOrgId());
           curDO.setSellerName(organizationDO.getFullName());
           curDO.setSellerCreditCode(organizationDO.getCreditCode());
           curDO.setOrgId(uc.getOrgId());
        }
        return curDO;
    }

    @ApiOperation(value = "获取销方信息", notes = "根据销方信息id获取开销方信息")
    @ApiImplicitParam(name = "id", value = "销方信息id", dataType = "long", paramType = "path", required = true)
    @RequestMapping(value = "/sellerInfo/{id}", method = RequestMethod.GET)
    public SellerInfoDO get(@PathVariable Long id) {
        SellerInfoDO curDO = sellerInfoService.selectById(id);
        if (curDO == null) {
            throw new SystemRuntimeException(ErrorCode.NOTEXISTS, "销方信息id");
        }
        return curDO;
    }

    @ApiOperation(value = "新增修改销方信息")
    @RequestMapping(value = "/sellerInfo", method = RequestMethod.PUT)
    public void updateSeller(@RequestBody SellerInfoVo sellerInfoVO) {
        SellerInfoDO sellerInfoDO = new SellerInfoDO();
        BeanUtils.copyProperties(sellerInfoVO, sellerInfoDO);
        if(sellerInfoDO.getId() == null){
            sellerInfoService.insert(sellerInfoDO);
        }else{
            sellerInfoService.updateById(sellerInfoDO);
        }
    }

    @ApiOperation(value = "获取授权地址", notes = "获取授权地址")
    @RequestMapping(value = "/sellerAuthUrl", method = RequestMethod.GET)
    public String getAuthUrl() {
        return invoiceHelper.getAUthUrl();
    }

}
